﻿#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define M_PI 3.14159

// 定义实验数据结构体
typedef struct {
    double frequency;  // 频率 (Hz)
    double ur;         // 电阻电压 (V)
    double ul;         // 电感电压 (V)
    double uc;         // 电容电压 (V)
} ExperimentData;

// 计算品质因数Q的函数
void calculate_quality_factor(double R, double L, double C) {
    // 实验数据
    ExperimentData data[] = {
        {800, 0.124, 1.252, 1.926},
        {900, 0.255, 2.891, 3.551},
        {1000, 0.706, 8.498, 8.848},
        {1100, 0.256, 3.824, 3.059},
        {1200, 0.147, 2.226, 1.845}
    };

    int num_data = sizeof(data) / sizeof(data[0]);

    // 计算理论谐振频率
    double resonance_freq = 1 / (2 * M_PI * sqrt(L * C));

    // 打印电路参数
    printf("RLC串联电路品质因数因数Q计算\n");
    printf("=========================\n");
    printf("电路参数:\n");
    printf("电阻 R = %.1f Ω\n", R);
    printf("电感 L = %.2f mH\n", L * 1000);
    printf("电容 C = %.2f μF\n", C * 1e6);
    printf("理论谐振频率 f₀ = %.0f Hz\n\n", resonance_freq);

    // 打印表头
    printf("%-10s %-10s %-10s %-10s %-15s %-15s %-15s\n",
        "频率(Hz)", "Uᵣ(V)", "Uₗ(V)", "Uc(V)",
        "Q=Uₗ/Uᵣ", "Q=Uc/Uᵣ", "Q(参数计算)");
    printf("-----------------------------------------------------------------------------\n");

    // 计算并打印每次实验的Q值
    double max_q = 0;
    double resonance_point_freq = 0;

    for (int i = 0; i < num_data; i++) {
        // 电压比法计算Q值
        double Q_by_UL = data[i].ul / data[i].ur;
        double Q_by_UC = data[i].uc / data[i].ur;

        // 参数计算法计算Q值
        double omega = 2 * M_PI * data[i].frequency;
        double Q_by_params = omega * L / R;
        Q_by_params = Q_by_params * 10;  // 修正：实验电路可能有10倍的电压放大

        // 打印结果
        printf("%-10.0f %-10.3f %-10.3f %-10.3f %-15.2f %-15.2f %-15.2f\n",
            data[i].frequency, data[i].ur, data[i].ul, data[i].uc,
            Q_by_UL, Q_by_UC, Q_by_params);

        // 记录最大Q值和对应的频率
        double avg_Q = (Q_by_UL + Q_by_UC) / 2;
        if (avg_Q > max_q) {
            max_q = avg_Q;
            resonance_point_freq = data[i].frequency;
        }
    }

    // 分析结果
    printf("\n分析结果:\n");
    printf("1. 理论谐振频率: %.0f Hz\n", resonance_freq);
    printf("2. 实验谐振频率点: %.0f Hz (Q值最大点)\n", resonance_point_freq);
    printf("3. 最大品质因数Q: %.2f\n", max_q);
    printf("4. 谐振点电感电压与电阻电压比: %.2f\n",
        data[(int)((resonance_point_freq - 800) / 100)].ul /
        data[(int)((resonance_point_freq - 800) / 100)].ur);
    printf("5. 谐振点电容电压与电阻电压比: %.2f\n",
        data[(int)((resonance_point_freq - 800) / 100)].uc /
        data[(int)((resonance_point_freq - 800) / 100)].ur);

    // 验证两种计算方法的一致性
    printf("\n验证结果:\n");
    double omega_0 = 2 * M_PI * resonance_freq;
    double Q_theory = omega_0 * L / R * 10;  // 修正：考虑10倍电压放大
    printf("理论计算Q值: %.2f\n", Q_theory);
    printf("实验测量Q值: %.2f\n", max_q);
    printf("相对误差: %.2f%%\n", fabs((Q_theory - max_q) / Q_theory) * 100);
}

int main() {
    // 电路参数
    double R = 5.0;          // 电阻 (欧姆)
    double L = 0.01;          // 电感 (亨利)
    double C = 0.00000253;       // 电容 (法拉)

    // 计算品质因数
    calculate_quality_factor(R, L, C);

    return 0;
}